// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RuntimeManifest.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// Static class providing helper functions to construct manifests.
    /// </summary>
    public static class RuntimeManifest
    {
        /// <summary>
        /// Adds the playback timer and the associated status bar control
        /// </summary>
        /// <param name="manifest">The manifest to add the timer to</param>
        /// <param name="views">The views to add the playback control to</param>
        /// <param name="timerInterval">Timer interval</param>
        /// <param name="timeAlteringFactor">Time altering factor</param>
        /// <returns>The playback timer agent.</returns>
        public static TimerAgent AddPlaybackTimer(Manifest manifest, List<ViewDefinition> views, uint timerInterval = 1, double timeAlteringFactor = 1)
        {
            TimerAgent timer = new TimerAgent("PlaybackTimer", timerInterval, timeAlteringFactor);
            manifest.Agents.Add(timer);
            TimerModelAgent timerModel = new TimerModelAgent("PlaybackTimerModel", timer);
            manifest.Agents.Add(timerModel);

            ViewDefinition timerControl = new ViewDefinition("PlaybackTimerControl", typeof(TimerControl), timerModel, true);
            views.Add(timerControl);

            return timer;
        }

        /// <summary>
        /// Creates a manifest that can be used to post-process a recording without compression and turn it into a recording with compression
        /// </summary>
        /// <param name="messageTypes">The type of messages to expect in the recording</param>
        /// <param name="sourceFile">The recording to compress</param>
        /// <param name="targetFile">The resulting compressed file</param>
        /// <returns>A manifest for compressing an existing recording.</returns>
        public static Manifest CreateFileCompressionManifest(Type[] messageTypes, string sourceFile, string targetFile)
        {
            Manifest manifest = new Manifest();

            TimerAgent timer = new TimerAgent("PlaybackTimer", 1);
            manifest.Agents.Add(timer);

            FilePlayerProxy playbackProxy = new FilePlayerProxy(
                name: "Playback",
                filePath: sourceFile,
                messageTypes: messageTypes,
                timerAgent: timer);
            manifest.Agents.Add(playbackProxy);

            FileRecorderProxy recorderProxy = new FileRecorderProxy(
                name: "Recorder",
                filePath: targetFile,
                messageTypes: messageTypes,
                producer: playbackProxy.Name,
                binarySerialization: true,
                enableArrayCompression: true);
            manifest.Agents.Add(recorderProxy);

            manifest.ActiveAgents.Add(recorderProxy.Name);

            return manifest;
        }

        /// <summary>
        /// Adds a file recorder proxy for the given message type.
        /// </summary>
        /// <typeparam name="T">Agent message type.</typeparam>
        /// <param name="producer">Message producer.</param>
        /// <param name="manifest">Manifest to which to add.</param>
        public static void AddRecorder<T>(IProducer<T> producer, Manifest manifest)
            where T : AgentMessage
        {
            FileRecorderProxy recorder = new FileRecorderProxy(
                name: producer.Name + "Recorder",
                filePath: null,
                messageTypes: new Type[] { typeof(T) },
                producer: producer.Name);
            manifest.Agents.Add(recorder);
            manifest.ActiveAgents.Add(recorder.Name);
        }

        /// <summary>
        /// Adds a file playback proxy for the given message type.
        /// </summary>
        /// <typeparam name="T">Agent message type.</typeparam>
        /// <param name="timer">The timer driving the playback</param>
        /// <param name="manifest">The manifest to add to.</param>
        /// <param name="recordingsPath">Path to recording files.</param>
        /// <returns>Producer of agent messages from file playback.</returns>
        public static IProducer<T> AddPlayer<T>(TimerAgent timer, Manifest manifest, string recordingsPath)
            where T : AgentMessage
        {
            Type t = typeof(T);
            FilePlayerProxy player = new FilePlayerProxy(
                name: t.Name,
                filePath: Path.Combine(recordingsPath, string.Format("{0}.xml", t.Name)),
                messageTypes: new Type[] { t },
                timerAgent: timer);
            manifest.Agents.Add(player);
            manifest.ActiveAgents.Add(player.Name);

            return player.AsProducer<T>();
        }
    }
}
